package com.itheima.web; /**
 * @author tongchen
 * @create 2022-11-30 19:09
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/aServlet")
public class CookieDemoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建cookie对象
        String s="张三" ;
        //进行编码
        String encode = URLEncoder.encode(s,"utf-8");
        Cookie cookie = new Cookie("userName", encode);
        //创建时间设置
        cookie.setMaxAge(60*60*24*7);
        //2.将cookie发往客户端
        response.addCookie(cookie);


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
